﻿using UnityEditor;
using UnityEngine;
using System.Collections;
using Sirenix.OdinInspector.Editor;
using Sirenix.OdinInspector;
using System.Collections.Generic;
using DanMuHelper.Tools;

public class FixAnimationPath : OdinEditorWindow
{
    [Header("层级切换目标")]
    public GameObject TargetLayerObject;

    [Header("需要切换的动画片段列表")]
    public List<AnimationClip> OrignalAnimations;

    [MenuItem("Tools/Animation/Fix Animation Path")]
    private static void FixAnimationPathMethod()
    {
        GetWindow<FixAnimationPath>().Show();
    }

    [Button]
    public void DoFix()
    {
        if (TargetLayerObject == null)
        {
            this.ShowNotification(new GUIContent("Change Error ：== 层级切换目标为空"));
            return;
        }

        if (OrignalAnimations != null || OrignalAnimations.Count > 0)
        {
            if (TargetLayerObject.TryGetComponent(out Animation animation))
            {
                animation.RemoveComponent<Animation>();
            }
            animation = TargetLayerObject.AddComponent<Animation>();

            for (int a = 0; a < OrignalAnimations.Count; a++)
            {
                AnimationClip ac = OrignalAnimations[a];

                if (ac != null)
                {
                    GameObject root = TargetLayerObject;
                    //获取所有绑定的EditorCurveBinding(包含path和propertyName)
                    EditorCurveBinding[] bindings = AnimationUtility.GetCurveBindings(ac);

                    for (int i = 0; i < bindings.Length; ++i)
                    {
                        EditorCurveBinding binding = bindings[i];

                        GameObject bindObj = AnimationUtility.GetAnimatedObject(root, binding) as GameObject;

                        if (bindObj == null)
                        {
                            bindObj = FindInChildren(root, binding.path);
                            if (bindObj)
                            {
                                string newPath = AnimationUtility.CalculateTransformPath(bindObj.transform, root.transform);
                                AnimationCurve curve = AnimationUtility.GetEditorCurve(ac, binding);
                                AnimationUtility.SetEditorCurve(ac, binding, null);
                                binding.path = newPath;
                                AnimationUtility.SetEditorCurve(ac, binding, curve);
                            }
                        }
                    }
                    animation.AddClip(ac, ac.name);
                    this.ShowNotification(new GUIContent("切换成功"));
                }
            }
            animation.playAutomatically = false;
            animation.animatePhysics = false;
            animation.cullingType = AnimationCullingType.BasedOnRenderers;
        }
        else
        {
            this.ShowNotification(new GUIContent("Change Error ：== 需要切换的动画片段为空或没有列表长度为0"));
        }
    }

    GameObject FindInChildren(GameObject obj, string goName)
    {
        Transform objTransform = obj.transform;

        GameObject finded = null;
        Transform findedTransform = objTransform.Find(goName);

        if (findedTransform == null)
        {
            for (int i = 0; i < objTransform.childCount; ++i)
            {
                finded = FindInChildren(objTransform.GetChild(i).gameObject, goName);
                if (finded)
                {
                    return finded;
                }
            }

            return null;
        }

        return findedTransform.gameObject;
    }

    //void OnGUI()
    //{
    //    Debug.Log("Fix AnimationClip");
    //    EditorGUILayout.LabelField("TargetRoot");
    //    target = EditorGUILayout.ObjectField(target, typeof(GameObject), true) as GameObject;

    //    EditorGUILayout.LabelField("AnimationClip");
    //    ac = EditorGUILayout.ObjectField(ac, typeof(AnimationClip), true) as AnimationClip;


    //    if (GUILayout.Button("Fix", GUILayout.Width(200)))
    //    {
    //        if (this.DoFix())
    //        {
    //            this.ShowNotification(new GUIContent("Change Complete"));
    //        }
    //        else
    //        {
    //            this.ShowNotification(new GUIContent("Change Error " + error));
    //        }
    //    }
    //}

}